using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    [SerializeField] private float time;
    [SerializeField] private Rigidbody2D rb;

    private void OnEnable()
    {
        // 
        // rb = GetComponent<Rigidbody2D>();
        // 设置速度
        // rb.velocity = new Vector2(this.transform.localScale.x * 10f, 0);
        // 等待time后自动启动销毁
        Invoke("RemoveBullet", time);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other == null)
            return;
        if (other.CompareTag("Enemy"))
        {
            IAttack attack = other.gameObject.GetComponent<IAttack>();
            attack.Attack();
            RemoveBullet();
        }
    }
    
    //
    private void RemoveBullet()
    {
        // 物体没有激活，直接返回
        if(!this.gameObject.activeSelf)
            return;
        // 播放爆炸音效
        AudioManager.Instance.PlayAudioSourceSFX(AudioSFXPath.PROP_BULLET_EXPLOSION, this.gameObject);
        // 取消激活
        this.gameObject.SetActive(false);
        // 放回对象池
        GenericsObjectPoolManager<GameObject>.Instance.AddObjectPool(this.gameObject);
    }
}
